package com.work.straighttalk.model;

import android.util.Log;

import com.hyphenate.chat.EMCallStateChangeListener;
import com.hyphenate.chat.EMClient;
import com.work.straighttalk.model.i.OnVideoCallListener;
import com.work.straighttalk.model.i.OnVoiceCallListener;
import com.work.straighttalk.model.i.VideoCallMI;

/**
 * Created by dllo on 16/3/14.
 */
public class VideoCallMimpl implements VideoCallMI {

    @Override
    public void isCall(String username, boolean isInCommintCall, OnVideoCallListener listener) {
        if (!isInCommintCall) {//拨打电话
//            soundPool = new SoundPool(1, AudioManager.STREAM_RING, 0);
//            comingBtnContainer.setVisibility(View.INVISIBLE);
//            hangupBtn.setVisibility(View.VISIBLE);
//            st1 = "正在连接对方...";
//            callStateTextView.setText(st1);
//            handler.sendEmptyMessage(MSG_CALL_MAKE_VOICE);
            Log.d("VoiceCallMImpl", "拨打电话");
            listener.callPhone();
        } else {//有电话进来
            Log.d("VoiceCallMImpl", "有电话进来");
//            voiceContronlLayout.setVisibility(View.INVISIBLE);
//            Uri ringUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
//            audioManager.setMode(AudioManager.MODE_RINGTONE);
//            audioManager.setSpeakerphoneOn(true);
//            ringtone = RingtoneManager.getRingtone(this, ringUri);
//            ringtone.play();
            listener.mComingCall();
        }

    }

    @Override
    public void conversation(final OnVideoCallListener listener) {

        EMClient.getInstance().callManager().addCallStateChangeListener(new EMCallStateChangeListener() {
            @Override
            public void onCallStateChanged(CallState callState, CallError callError) {
                switch (callState) {
                    case CONNECTING://正在连接对方
                        listener.mCinnecting();
                        break;
                    case CONNECTED://双方已经建立连接
                        Log.d("VoiceCallMImpl", "双方已经建立连接");
                        listener.mConnected();
                        break;
                    case ACCEPTED://电话已经接通
                        listener.mAcceptend();
                        Log.d("VoiceCallMImpl", "电话已经接通");
                        break;
                    case DISCONNNECTED://电话断了
                        Log.d("VoiceCallMImpl", "电话断了");
                        break;
                }
            }
        });

    }
}
